package com.openspring.platform.controller;

import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.openspring.framework.common.Paging;
import com.openspring.framework.controller.GenericController;
import com.openspring.framework.util.ResultUtils;
import com.openspring.platform.model.Resource;
import com.openspring.platform.model.node.ResourceNode;
import com.openspring.platform.service.ResourceService;

@Controller
@RequestMapping("resource")
public class ResourceController extends GenericController<Resource, Long>{

	private ResourceService resourceService;

	@javax.annotation.Resource(name="resourceService")
	public void setResourceService(ResourceService resourceService) {
		super.setGenericService(resourceService);
		this.resourceService = resourceService;
	}
	
	@RequestMapping(value="/create", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> create(@ModelAttribute Resource entity) {
		resourceService.create(entity);
		return ResultUtils.data(entity);
	}

	@RequestMapping(value="/modify", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modify(@ModelAttribute Resource entity) {
		resourceService.modify(entity);
		return ResultUtils.data(entity);
	}
	
	@RequestMapping(value="/remove", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> remove(@RequestParam("id") Long[] id) {
		if(id.length == 1)
			resourceService.remove(id[0]);
		else
			resourceService.remove(id);
		return ResultUtils.success();
	}
	
	@RequestMapping(value="/load/{id}", method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> load(@PathVariable("id") Long id) {
		return ResultUtils.data(resourceService.load(id));
	}
	
	@RequestMapping(value="/list", method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list(
			@ModelAttribute Paging paging,
			@RequestParam(value="filter", required=false) String filter,
			@RequestParam(value="sort", required=false) String sort) {
		
		return this.doQuery(paging, filter, sort);
	}
	
	@RequestMapping(value="/query", method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> query(@RequestParam(value="id") Long id) {
		return ResultUtils.tree(resourceService.findEnableChildren(id));
	}
	
	@RequestMapping(value="/stree", method=RequestMethod.GET)
	@ResponseBody
	public ResourceNode stree() {
		return resourceService.getResourceTree();
	}
	
	@RequestMapping(value="/dtree", method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> dtree(@RequestParam(value="id") Long id) {
		return ResultUtils.tree(resourceService.findChildren(id));
	}
}